package com.system.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.product.domain.Product;
import com.system.product.service.ProductService;
import com.system.product.mapper.ProductMapper;
import com.system.product.vo.ProductVO;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author Admin
* @description 针对表【product】的数据库操作Service实现
* @createDate 2024-12-10 11:11:50
*/
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
    implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Override
    public ProductVO findDetailById(long productId) {

        Product product = productMapper.selectById(productId);
        System.out.println("product: "+product);

        return beanProcess(product);
    }

    @Override
    public List<ProductVO> findProductsByIdBatch(List<Long> productIdList) {
        List<Product> productDOList =  productMapper.selectList(new QueryWrapper<Product>().in("id",productIdList));

        List<ProductVO> productVOList = productDOList.stream().map(obj->beanProcess(obj)).collect(Collectors.toList());

        return productVOList;
    }

    private ProductVO beanProcess(Product product) {

        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(product,productVO);
        productVO.setStock( product.getStock() - product.getLockStock());
        return productVO;
    }
}




